#include "WifiInfoDelegate.h"
#include <QPainter>
#include <QModelIndex>
#include <QDebug>
#include <QEvent>
#include <QMovie>

WifiInfoDelegate::WifiInfoDelegate()
{

}

void WifiInfoDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    //处理wifi列表信息
    QString wifiName = index.data().toString().split("|").value(0);
    int signal = index.data().toString().split("|").value(1).toInt();
    int lock = index.data().toString().split("|").value(2).toInt();
    QString curWifiName =  index.data().toString().split("|").value(3);
    int isconnected = index.data().toString().split("|").value(4).toInt();

    //绘制背景色
    QStyleOptionViewItemV4 ov4 = option;
    initStyleOption(&ov4, index);
    if (ov4.state & QStyle::State_Selected )
    {
        painter->fillRect(option.rect,Qt::gray);
    }else
    {
        QBrush brush(QColor(33,33,33));
        painter->fillRect(option.rect,brush);
    }

    //如果wifi加密，绘制加密图标
    if(lock==1)
    {
        QPixmap pix("://resource/images/optionPage/setWifi/lock.png");
        painter->drawPixmap(QPoint(option.rect.center().x()-150,option.rect.center().y()-pix.height()/2),pix);
    }

    //绘制wifi信号

    switch (signal) {
    case 1:
    {
        QPixmap pix("://resource/images/optionPage/setWifi/wifi1.png");
        painter->drawPixmap(QPoint(option.rect.center().x()-120,
                                   option.rect.center().y()-pix.height()/2),
                            pix);
    }
        break;
    case 2:
    {
        QPixmap pix("://resource/images/optionPage/setWifi/wifi2.png");
        painter->drawPixmap(QPoint(option.rect.center().x()-120,
                                   option.rect.center().y()-pix.height()/2),
                            pix);
    }
        break;
    case 3:
    {
        QPixmap pix("://resource/images/optionPage/setWifi/wifi3.png");
        painter->drawPixmap(QPoint(option.rect.center().x()-120,
                                   option.rect.center().y()-pix.height()/2),
                            pix);
    }
        break;
    case 4:
    {
        QPixmap pix("://resource/images/optionPage/setWifi/wifi4.png");
        painter->drawPixmap(QPoint(option.rect.center().x()-120,
                                   option.rect.center().y()-pix.height()/2),
                            pix);
        break;
    }
    default:
        break;
    }

    //绘制wifi名称
    QRect rect = option.rect;
    rect.moveLeft(355);
    QTextOption o(Qt::AlignLeft|Qt::AlignVCenter);
    QFont font;
    font.setPixelSize(26);
    painter->setFont(font);
    painter->drawText(rect,wifiName,o);

    //绘制连接状态
    if(curWifiName==wifiName)
    {
        if(isconnected == 2)//正在连接
        {

        }else if(isconnected == 1)
        {
            rect.moveLeft(670);

            painter->drawText(rect,"已连接 >",o);
        }else
        {
            rect.moveLeft(670);
            painter->drawText(rect,"无法联网 >",o);
        }
    }
}

QSize WifiInfoDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
    Q_UNUSED(option)
    Q_UNUSED(index)
    return QSize(835,80);
}

